package com.cd.servlet;

import cn.hutool.json.JSONUtil;
import com.cd.dao.MovieDao;
import com.cd.entity.Movie;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/movie")
public class MovieServlet extends HttpServlet {

	private MovieDao movieDao;

	@Override
	public void init() throws ServletException {
		movieDao = new MovieDao();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		String cmd = req.getParameter("cmd");
		if (StringUtils.isNotBlank(cmd)) {
			if ("list".equals(cmd)) {
				list(req, resp);
			} else if ("findByPage".equals(cmd)) {
				findByPage(req, resp);
			} else if ("search".equals(cmd)) {
				search(req, resp);
			} else if ("add".equals(cmd)) {
				add(req, resp);
			} else if ("update".equals(cmd)) {
				update(req, resp);
			} else if ("delete".equals(cmd)) {
				delete(req, resp);
			}
		}
	}

	private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		List<Movie> movies = movieDao.findAll();
		String json = JSONUtil.toJsonStr(movies);
		resp.getWriter().write(json);
		resp.getWriter().flush();
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("pageSize"));
		PageInfo<Movie> pageInfo = movieDao.findByPage(page, pageSize);
		String json = JSONUtil.toJsonStr(pageInfo);
		resp.getWriter().write(json);
		resp.getWriter().flush();
	}

	private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("pageSize"));
		Movie movie = new Movie();
		movie.setTitle(req.getParameter("title"));
		movie.setDirector(req.getParameter("director"));
		PageInfo<Movie> pageInfo = movieDao.search(page, pageSize, movie);
		String json = JSONUtil.toJsonStr(pageInfo);
		resp.getWriter().write(json);
		resp.getWriter().flush();
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Movie movie = new Movie();
		movie.setMovieId(Long.parseLong(req.getParameter("movieId")));
		movie.setTitle(req.getParameter("title"));
		movie.setDirector(req.getParameter("director"));
		movie.setReleaseDate(req.getParameter("releaseDate"));
		movie.setRating(Double.parseDouble(req.getParameter("rating")));
		Map<String, String> map = new HashMap<>();
		try {
			movieDao.add(movie);
			map.put("code", "200");
			map.put("message", "添加电影成功！");
		} catch (IOException e) {
			map.put("code", "500");
			map.put("message", "添加电影失败！");
		}
		resp.getWriter().write(JSONUtil.toJsonStr(map));
		resp.getWriter().flush();
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Movie movie = new Movie();
		movie.setMovieId(Long.parseLong(req.getParameter("movieId")));
		movie.setTitle(req.getParameter("title"));
		movie.setDirector(req.getParameter("director"));
		movie.setReleaseDate(req.getParameter("releaseDate"));
		movie.setRating(Double.parseDouble(req.getParameter("rating")));
		Map<String, String> map = new HashMap<>();
		try {
			movieDao.update(movie);
			map.put("code", "200");
			map.put("message", "修改电影成功！");
		} catch (IOException e) {
			map.put("code", "500");
			map.put("message", "修改电影失败！");
		}
		resp.getWriter().write(JSONUtil.toJsonStr(map));
		resp.getWriter().flush();
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String movieIds = req.getParameter("movieIds");
		String[] split = movieIds.split(",");
		movieDao.delete(split);
		Map<String, String> map = new HashMap<>();
		map.put("code", "200");
		map.put("message", "删除电影成功！");
		resp.getWriter().write(JSONUtil.toJsonStr(map));
		resp.getWriter().flush();
	}
}